package com.kma.summer2012.client.presenters.authorization;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.Crypto;
import com.kma.summer2012.client.events.authorization.LoginEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.user.CrumbsUserService;
import com.kma.summer2012.shared.model.UICrumbsUser;

/*import com.googlecode.gwt.crypto.bouncycastle.DataLengthException;
import com.googlecode.gwt.crypto.bouncycastle.InvalidCipherTextException;
import com.googlecode.gwt.crypto.client.TripleDesCipher;*/

public class PasswordRecoveryPresenter implements Presenter {

    public interface Display {
        HasClickHandlers getCancelButton();

        HasClickHandlers getContinueButton();

        String getMailBox();

        void hideAll();

        void setMailBox(String text);

        void setTextResult(String textResult);

        Widget asWidget();
    }

    private final Display display;

    public PasswordRecoveryPresenter(Display display) {
        this.display = display;
        bind();
    }

    public void bind() {
        this.display.getCancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new LoginEvent());
            }
        });
        this.display.getContinueButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doCheckLogin();
            }
        });
    }

    private void doCheckLogin() {
        if (testEmail(this.display.getMailBox())) {

            CrumbsUserService.App.getInstance().getByLogin(this.display.getMailBox(), new AsyncCallback<UICrumbsUser>() {
                @Override
                public void onFailure(Throwable throwable) {
                    display.setTextResult("User with such e-mail does not exist!");
                }

                @Override
                public void onSuccess(UICrumbsUser uiCrumbsUser) {
                    String encrypted = Crypto.encrypt(Integer.toString(uiCrumbsUser.getUserId()));
                    String path = "http://" + Window.Location.getHost() + Window.Location.getPath() + Window.Location.getQueryString() + "#changePassword?id=" + encrypted;
                    CrumbsUserService.App.getInstance().sendMailRecovery(uiCrumbsUser.getLogin(), path, new AsyncCallback<Boolean>() {
                        @Override
                        public void onFailure(Throwable throwable) {
                            throw new RuntimeException(throwable);
                        }

                        @Override
                        public void onSuccess(Boolean o) {
                            if (o) {
                                display.hideAll();
                            } else {
                                display.setTextResult("No such user");
                            }
                        }
                    });
                }
            });
        } else {
            display.setTextResult("E-mail not valid!");
        }
    }

    public boolean testEmail(String email) {
        return email.matches("^\\S+@\\S+\\.\\S+$");
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

}